const LOCAL_STORAGE_KEY_LOCAL = "config.local";
export const LOCAL_EN = "en";
export const LOCAL_ZH = "zh";
// DICT
const dict = {
  ["component_overlay_confirm"]: {
    default: "Confirm",
    zh: "确认",
  },
  ["component_overlay_cancel"]: {
    default: "Cancel",
    zh: "取消",
  },
  ["mainframe_button_top_left"]: {
    default: "TileSelector",
    zh: "图块选择器",
  },
  ["mainframe_button_top_center"]: {
    default: "TileEditor",
    zh: "图块编辑器",
  },
  ["mainframe_button_top_right"]: {
    default: "MapEditor",
    zh: "地图编辑器",
  },
  ["tile_selector_top_tips"]: {
    default: "Click to select tile.",
    zh: "点击选择图块",
  },
  ["tile_editor_top_tips"]: {
    default: "Primary mouse button to draw, second mouse button to clear.",
    zh: "使用鼠标主键绘制图案, 鼠标次键来清除图案",
  },
  ["map_editor_top_tips"]: {
    default: "Primary mouse button to draw select tile, second mouse button to draw tile 0.",
    zh: "使用鼠标主键绘制选定的图块, 鼠标次键来绘制0号图块",
  },
  ["map_editor_op_button_new_map"]: {
    default: "New Map",
    zh: "新建地图",
  },
  ["map_editor_op_button_open_map"]: {
    default: "Open Map",
    zh: "打开地图",
  },
  ["map_editor_op_button_save_map"]: {
    default: "Save Map",
    zh: "保存地图",
  },
  ["map_editor_op_button_scale"]: {
    default: "Scale",
    zh: "缩放",
  },
  ["map_editor_text_prompt_new_map"]: {
    default: "Are you sure want to create new map? current map will lost without save.",
    zh: "确定要新建地图吗？当前地图的数据将会丢失。",
  },
  ["map_editor_text_prompt_map_width"]: {
    default: "Please set map width. (1~256, recommend less then 64)",
    zh: "请设置地图宽度 (1~256, 建议小于64)",
  },
  ["map_editor_text_prompt_map_height"]: {
    default: "Please set map height. (1~256, recommend less then 64)",
    zh: "请设置地图高度 (1~256, 建议小于64)",
  },
  ["map_editor_text_prompt_open_map"]: {
    default: "Are you sure want to open another map? current map will lost without save.",
    zh: "确定要打开地图吗？当前地图的数据将会丢失。",
  },
  ["map_editor_text_prompt_map_has_info"]: {
    default: "Is the file has basic map info? (First two bytes are width and height, from 0~255, means 1~256)",
    zh: "文件中包含地图基本信息吗？(前两字节分别代表宽和高, 0~255代表1~256)",
  },
  ["map_editor_text_prompt_choose_filename"]: {
    default: "Please choose a filename (e.g. world)",
    zh: "请输入一个文件名 (例如: world)",
  },
  ["map_editor_text_prompt_map_with_info"]: {
    default: "Do you want save with basic map info? (First two bytes are width and height, from 0~255, means 1~256)",
    zh: "你想要在文件中包含地图的基本信息吗？(前两字节分别代表宽和高, 0~255代表1~256)",
  },
  ["map_editor_text_prompt_map_save_format"]: {
    default: "Please choose a format (file, base64, python)",
    zh: "请选择保存的格式 (file, base64, python)",
  },
  ["map_editor_text_alert_copy"]: {
    default: "Copy to clipboard",
    zh: "复制到剪贴板",
  },
  ["map_editor_text_error_map_size_error"]: {
    default: "Wrong map size!",
    zh: "地图尺寸错误！",
  },
  ["map_editor_text_error_map_create_failed"]: {
    default: "Create new map failed.",
    zh: "创建地图失败。",
  },
  ["map_editor_text_error_map_open_failed"]: {
    default: "Open map failed.",
    zh: "打开地图失败。",
  },
  ["tile_selector_op_btn_new"]: {
    default: "New Tiles",
    zh: "新建图块集",
  },
  ["tile_selector_op_btn_open"]: {
    default: "Open Tiles",
    zh: "打开图块集",
  },
  ["tile_selector_op_btn_save"]: {
    default: "Save Tiles",
    zh: "保存图块集",
  },
  ["tile_selector_text_prompt_new_tile_map"]: {
    default: "Are you sure want to create new tile map? current map will lost without save.",
    zh: "确定要新建图块集吗？当前图块集的数据将会丢失。",
  },
  ["tile_selector_text_prompt_tile_map_width"]: {
    default: "Please set tile width. (8~64, recommend less then 16, must be multiply of 8)",
    zh: "请设置图块宽度 (8~64, 建议小于16, 必须是8的倍数)",
  },
  ["tile_selector_text_prompt_tile_map_height"]: {
    default: "Please set tile height. (8~64, recommend less then 16)",
    zh: "请设置图块高度 (8~64, 建议小于16)",
  },
  ["tile_selector_text_prompt_tile_map_count"]: {
    default: "Please set tile count. (8~256, recommend less then 128)",
    zh: "请设置图块数量 (8~256, 建议小于128)",
  },
  ["tile_selector_text_prompt_open_tile_map"]: {
    default: "Are you sure want to open another tile map? current map will lost without save.",
    zh: "确定要打开图块集吗？当前地图的数据将会丢失。",
  },
  ["tile_selector_text_prompt_map_has_info"]: {
    default: "Is the file has basic tile map info? (First two bytes are width and height)",
    zh: "文件中包含图块基本信息吗？(前两字节分别代表宽和高)",
  },
  ["tile_selector_text_prompt_choose_filename"]: {
    default: "Please choose a filename (e.g. world)",
    zh: "请输入一个文件名 (例如: world)",
  },
  ["tile_selector_text_prompt_tile_map_with_info"]: {
    default: "Do you want save with basic tile map info? (First two bytes are tiles` width and height)",
    zh: "你想要在文件中包含图块集的基本信息吗？(前两字节分别代表图块宽和高)",
  },
  ["tile_selector_text_prompt_tile_map_save_format"]: {
    default: "Please choose a format (file, base64, python)",
    zh: "请选择保存的格式 (file, base64, python)",
  },
  ["tile_selector_text_alert_copy"]: {
    default: "Copy to clipboard",
    zh: "复制到剪贴板",
  },
  ["tile_selector_text_error_tile_map_size_error"]: {
    default: "Wrong tile size!",
    zh: "图块尺寸错误！",
  },
  ["tile_selector_text_error_tile_map_create_failed"]: {
    default: "Create new tile map failed.",
    zh: "创建图块集失败。",
  },
  ["tile_selector_text_error_tile_map_open_failed"]: {
    default: "Open tile map failed.",
    zh: "打开图块集失败。",
  },
  [""]: {
    default: "",
    zh: "",
  },
}

// utils
function getLocalStorage(name, defaultValue){
  let value = localStorage.getItem(name);
  if (value === null){
    return defaultValue;
  }else{
    return value;
  }
}

// translate function
const config = {
  local: getLocalStorage(LOCAL_STORAGE_KEY_LOCAL, LOCAL_EN),
}

export function setLocal(local){
  config.local = local;
  localStorage.setItem(LOCAL_STORAGE_KEY_LOCAL, local);
}

export function getText(name){
  let item = dict[name];
  if (item === undefined){ return "[No Text]"; }
  if (item[config.local] !== undefined){
    return item[config.local];
  }else{
    return item.default;
  }
}